<template>
	<view class="index">
		<view v-if="tabberPageLoadFlag[0]" :style="{display: currentIndex === 0 ? '' : 'none'}">
			<scroll-view class="custom-tabbar-page" :style="{height: scrollConfig.scrollHeight + 'px'}" scroll-y
				enable-back-to-top>
				<page-index :config="scrollConfig" ref="home-board"></page-index>
			</scroll-view>
		</view>

		<view v-if="tabberPageLoadFlag[1]" :style="{display: currentIndex === 1 ? '' : 'none'}">
			<scroll-view class="custom-tabbar-page" :style="{height: scrollConfig.scrollHeight + 'px'}" scroll-y
				enable-back-to-top>
				<page-device-control :config="scrollConfig" ref="home-board"></page-device-control>
			</scroll-view>
		</view>

		<view id="tabbarBox" :class="{ 'visible': isVisible }">
			<qms-tabbar :tabbarList="tabbarList" :currentIndex="currentIndex" @change="switchTabbar"></qms-tabbar>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { reactive, computed, onMounted, ref, nextTick, getCurrentInstance } from 'vue'
	import { onLoad, onShow } from '@dcloudio/uni-app'
	import { useMainStore, useQmsStore, useUserStore } from '@/store';
	const qmsStore = useQmsStore();
	const mainStore = useMainStore();
	const userStore = useUserStore();
	import useRequestQueue from '@/uni_modules/mc-ui/libs/http/UniversalRequestQueue';
	const { request, requestByUrl, onAllComplete, onError } = useRequestQueue();

	import pageIndex from "/st_qms/components/page_index/page_index";
	import pageDeviceControl from "/st_qms/components/page_index/page_device_control";
	import qmsTabbar from "/st_qms/components/qms-tabbar/qms-tabbar"

	// import {
	// 	getBluetooth,
	// 	getPermissUser,
	// 	getBluetoothState,
	// 	openBluetooth,
	// 	setPermissUser,
	// 	closeBluetooth,
	// 	setBluetoothStop,
	// 	setPairDevice,
	// 	offPairDevice,
	// 	getPairedDevices,
	// 	onBluetoothA2DP,
	// 	onBluetoothHFP,
	// 	onBluetoothHID,
	// 	setConnectAllowedProfiles,
	// 	offBluetoothA2DP,
	// 	offBluetoothHFP,
	// 	offBluetoothHID,
	// 	getEnerateRandomUUID,
	// 	onStartListen,
	// } from '@/uni_modules/an-bluetooth';

	// // const harmonyBle = uni.requireNativePlugin('an-bluetooth')
	// const getPermiss = async () => {
	// 	console.log(1111);
	// 	// let number = await harmonyBle.getPermissUser()

	// 	// 获取当前蓝牙适配器状态
	// 	let bleStart = await getBluetoothState();
	// 	console.log(bleStart);
	// 	// 检查蓝牙是否授权
	// 	let bleType = await getPermissUser();
	// 	console.log(bleType);

	// 	if (bleType != 0) {
	// 		// 获取蓝牙权限
	// 		let number = await setPermissUser();
	// 		console.log(number);
	// 	} else {
	// 		// openBluetooth()
	// 		// 开启扫描
	// 		getBluetooth({
	// 			count: 6,
	// 			onResult: (res) => {
	// 				console.log(res);
	// 			},
	// 			offResult: (res) => {
	// 				// console.log(res);
	// 			}
	// 		})
	// 	}


	// }

	// getPermiss()

	let tabberPageLoadFlag = ref<any>([]);
	let tabbarList = ref<any>([{
		id: 0,
		icon: '/static/assets/image/tabbar-icons/Index.png',
		selectIcon: '/static/assets/image/tabbar-icons/Index_Check.png',
		text: '首页',
		centerItem: false
	},
	{
		id: 1,
		icon: '/static/assets/image/tabbar-icons/control.png',
		selectIcon: '/static/assets/image/tabbar-icons/controlCheck.png',
		text: '智控',
		centerItem: false
	},
	{
		id: 2,
		icon: '/static/assets/image/tabbar-icons/add-white.png',
		selectIcon: '/static/assets/image/tabbar-icons/add-white.png',
		text: '',
		centerItem: true
	},
	{
		id: 3,
		icon: '/static/assets/image/tabbar-icons/stats.png',
		selectIcon: '/static/assets/image/tabbar-icons/statsCheck.png',
		text: '看板',
		centerItem: false
	},
	{
		id: 4,
		icon: '/static/assets/image/tabbar-icons/me.png',
		selectIcon: '/static/assets/image/tabbar-icons/me_Check.png',
		text: '我的',
		centerItem: false
	}
	]);
	let options = reactive<any>({});
	let currentIndex = ref(0);
	let safeAreaInsetBottom = ref(true);
	let scrollConfig = reactive<any>({
		scrollHeight: 500,
		tabbarHeight: 50
	});
	let isVisible = ref(false)


	let routes = getCurrentPages(); //当前页面栈
	// 存储当前页面栈初始位置信息
	mainStore.appConfigInfo.routePageCurrent = routes.length;



	// 根据底部tabbar菜单列表设置对应页面的加载情况
	tabberPageLoadFlag.value = tabbarList.value.map((item : any, tabbar_index : any) => {
		return 0 === item.id
	});
	const _switchTabbarPage = (current : number) => {

		const selectPageFlag = tabberPageLoadFlag.value[current];
		if (current == 0) {
			// 简单处理，当选中项为0时，将第二项的状态改为false
			tabberPageLoadFlag.value[1] = false;
		}

		if (selectPageFlag === undefined) {
			return
		}
		if (selectPageFlag === false) {
			tabberPageLoadFlag.value[current] = true
		}
		currentIndex.value = current;
	}
	const switchTabbar = (data : any) => {

		// if (index == 2) {

		//  uni.navigateTo({
		//      url: "/st_pages/mine/mine",
		//  });
		// } else {
		//  _switchTabbarPage(index)
		// }

		switch (data.id) {
			case 4: // 个人中心
				uni.navigateTo({
					url: "/st_pages/mine/mine",
				});
				break;
			case 3: // 商场 --> 暂时改为看板
				// uni.$utils.layer.alert("暂未开通此权限");
				uni.navigateTo({
					url: "/st_qms/page_index/kanban/stat_dashboard"
				});
				break;
			case 2: // 工作台
				uni.navigateTo({
					url: "/st_qms/page-workbench/page-workbench"
				});
				break;
			default:
				_switchTabbarPage(data.id);
				break;
		}
	}
	switchTabbar(tabbarList.value[0]);




	const instance = getCurrentInstance() as any;
	// 获取容器高度
	const getScollHeightFun = () => {
		// 总盒子高度
		const windowHeight = uni.$utils.getWindowInfo().windowHeight;
		const query = uni.createSelectorQuery().in(instance.proxy);
		query.select('#tabbarBox').boundingClientRect((data : any) => {

			let sysinfo = mainStore.systemConfig.sysDeviceInfo

			if (data) {
				let _tabbarHeight = data.height;

				_tabbarHeight += (sysinfo.safeAreaInsets.bottom / 2);


				let _scrollHeight = windowHeight - _tabbarHeight;
				scrollConfig.tabbarHeight = _tabbarHeight;
				scrollConfig.scrollHeight = _scrollHeight;
			} else {
				scrollConfig.tabbarHeight = 0;
				scrollConfig.scrollHeight = windowHeight;
			}
			isVisible.value = true;
		}).exec();

	}




	onMounted(async () => {
		await nextTick();
		setTimeout(() => {

			getScollHeightFun();
		}, 0)
	})
</script>

<style>

</style>